/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 */
public class Resultat3 extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            Class.forName(JDBC_DRIVER);
            String DATABASE_URL = "jdbc:mysql://localhost:3306/test_ikc019?user=root&password=admin";
            
            Connection conn = DriverManager.getConnection(DATABASE_URL);
            
            PreparedStatement p_statement;
            
            String[] val = request.getParameterValues("val");
            String[] text = request.getParameterValues("text");
            
            
            
            ResultSet resultatMängd = null;
            if(val[0].equals("album"))
            {   
            //    resultatMängd = statement.executeQuery("SELECT title from music_recordings where title like '" + text[0]+"%'");
                p_statement = conn.prepareStatement("SELECT title from music_recordings where title like ?");
                p_statement.clearParameters();
                p_statement.setString(1,text[0] +"%");
                /*p_statement.setString(2,"music_recordings");
                p_statement.setString(3,"title");*/
                resultatMängd = p_statement.executeQuery();
                //p_statement.close();
            }
            else if(val[0].equals("titel"))
            {
            //    resultatMängd = statement.executeQuery("SELECT title from music_tracks where title like '" + text[0]+"%'");
                 p_statement = conn.prepareStatement("SELECT title from music_tracks where title like ?");
                p_statement.clearParameters();
                p_statement.setString(1,text[0] +"%");
                /*p_statement.setString(2,"music_recordings");
                p_statement.setString(3,"title");*/
                resultatMängd = p_statement.executeQuery();
                //p_statement.close();
            }
            else if(val[0].equals("artnr"))
            {
             //   resultatMängd = statement.executeQuery("SELECT title from music_recordings where recording_id = " + text[0]);
                 p_statement = conn.prepareStatement("SELECT title from music_recordings where recording id = ?");
                p_statement.clearParameters();
                p_statement.setString(1,text[0]);
                /*p_statement.setString(2,"music_recordings");
                p_statement.setString(3,"title");*/
                resultatMängd = p_statement.executeQuery();
                //p_statement.close();
            }
            out.println("<html>");
            out.println("<head>");          
            out.println("</head>");
            out.println("<body>");
            out.println("<ul>");
             while(resultatMängd.next())
            {
                out.println("<li>" + resultatMängd.getObject(1) + "</li>");
            }
            out.println("</ul>");
            out.println("</body>");
            out.println("</html>");
            
             resultatMängd.close();
            
            conn.close();
        } 
        catch(ClassNotFoundException e)
        {
            out.println("Fel: " + e);
        }
        catch(SQLException e)
        {
            out.println("Fel: " + e);
        }
         finally { 
            out.close();
        }
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
